<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    div {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .container {
      width: 200px;
      height: 200px;
      background-color: red;
    }
    .inner {
      width: 100px;
      height: 100px;
      background-color: blue;
    }
    .btn {
      width: 50px;
      height: 50px;
      background-color: green;
    }
  </style>
  <body>
    <div class="container">
      container
      <div class="inner">
        inner
        <div class="btn">button</div>
      </div>
    </div>
  </body>
  <script>
    // 事件委托
    document
      .querySelector(".container")
      .addEventListener("click", function (e) {
        console.log("container");
      });
    document.querySelector(".inner").addEventListener(
      "click",
      function (e) {
        console.log("inner");
      },
      true
    ); // false表示冒泡阶段触发,如果使用true表示捕获阶段触发,那么点击btn的时候先出法inner 再出发btn 最后冒泡到container上。
    document.querySelector(".btn").addEventListener("click", function (e) {
      e.stopPropagation(); // 阻止事件冒泡 加上这个将不会再执行container的点击事件了。
      console.log("btn");
    });
  </script>
</html>
